package com.ruoyi.system.controller;

import com.ruoyi.system.domain.InvAccount;
import com.ruoyi.system.request.PostionAddListRequest;
import com.ruoyi.system.service.IInvAccountService;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InvPostion;
import com.ruoyi.system.service.IInvPostionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 持仓Controller
 * 
 * @author ruoyi
 * @date 2022-01-12
 */
@Controller
@RequestMapping("/book/postion")
public class InvPostionController extends BaseController
{
    private String prefix = "book/postion";

    @Autowired
    private IInvPostionService invPostionService;
    @Autowired
    private IInvAccountService invAccountService;

    @RequiresPermissions("book:postion:view")
    @GetMapping()
    public String postion( ModelMap mmap)
    {
        mmap.put("accs", invAccountService.selectInvAccountList(new InvAccount()));
        return prefix + "/postion";
    }

    /**
     * 查询持仓列表
     */
    @RequiresPermissions("book:postion:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InvPostion invPostion)
    {
//        if (invPostion.getSecAccount()==0){
//            invPostion.setSecAccount(null);
//        }
        startPage();
        List<InvPostion> list = invPostionService.selectInvPostionList(invPostion);
        return getDataTable(list);
    }

    /**
     * 导出持仓列表
     */
    @RequiresPermissions("book:postion:export")
    @Log(title = "持仓", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InvPostion invPostion)
    {
        List<InvPostion> list = invPostionService.selectInvPostionList(invPostion);
        ExcelUtil<InvPostion> util = new ExcelUtil<InvPostion>(InvPostion.class);
        return util.exportExcel(list, "持仓数据");
    }

    /**
     * 新增持仓
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存持仓
     */
    @RequiresPermissions("book:postion:add")
    @Log(title = "持仓", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InvPostion invPostion)
    {
        return toAjax(invPostionService.insertInvPostion(invPostion));
    }


    /**
     * 新增持仓
     */
    @GetMapping("/addList")
    public String addList( ModelMap mmap)
    {
        mmap.put("accs", invAccountService.selectInvAccountList(new InvAccount()));
        return prefix + "/addList";
    }

    /**
     * 新增保存持仓
     */
    @RequiresPermissions("book:postion:add")
    @Log(title = "持仓", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    @ResponseBody
    public AjaxResult addListSave(PostionAddListRequest invPostion)
    {
        return toAjax(invPostionService.insertInvPostionList(invPostion));
    }

    /**
     * 修改持仓
     */
    @RequiresPermissions("book:postion:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        InvPostion invPostion = invPostionService.selectInvPostionById(id);
        mmap.put("invPostion", invPostion);
        return prefix + "/edit";
    }

    /**
     * 修改保存持仓
     */
    @RequiresPermissions("book:postion:edit")
    @Log(title = "持仓", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InvPostion invPostion)
    {
        return toAjax(invPostionService.updateInvPostion(invPostion));
    }

    /**
     * 删除持仓
     */
    @RequiresPermissions("book:postion:remove")
    @Log(title = "持仓", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(invPostionService.deleteInvPostionByIds(ids));
    }
}
